/*
 * @Author: yangni 920556271@qq.com
 * @Date: 2024-01-14 13:53:51
 * @LastEditors: yangni 920556271@qq.com
 * @LastEditTime: 2024-05-15 14:18:42
 * @FilePath: /yt-devops/lib/pages/devices/device_tab/base_info_edit/base_info_edit_view.dart
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import 'package:bruno/bruno.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:yt_devops/components/app_bar/app_bar_widget.dart';
import 'package:yt_devops/components/brn/brn_bottom_button_panel.dart';
import 'package:yt_devops/components/card_wrap.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/yt_input.dart';
import 'package:yt_devops/resources/str_res_keys.dart';
import 'package:yt_devops/styles/styles.dart';
import 'package:yt_devops/utils/func_utils.dart';
import '../basic_info/base/base_basic_model.dart';
import 'base_info_edit_controller.dart';

class BaseInfoEditPage extends GetView<BaseInfoEditController> {
  BaseInfoEditPage({Key? key}) : super(key: key);

  final BrnFormItemConfig theme = BrnFormItemConfig(
    disableTextStyle:
        BrnTextStyle(color: Colour.mainTextColor, fontSize: 14.sp),
    contentTextStyle:
        BrnTextStyle(fontSize: 14.sp, color: Colour.mainTextColor),
    titlePaddingLg: const EdgeInsets.only(left: 0),
    titleTextStyle: BrnTextStyle(fontSize: 14.sp, color: Colour.otherTextColor),
    formPadding: EdgeInsets.symmetric(horizontal: 0, vertical: 13),
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBarWidget.defaultAppBar(controller.device?.name ?? ''),
      body: CardWrap(
        child: ListView.separated(
          shrinkWrap: true,
          padding: EdgeInsets.symmetric(horizontal: 16.w),
          physics: const NeverScrollableScrollPhysics(),
          itemCount: controller.infos.length,
          itemBuilder: (context, index) {
            var item = controller.infos[index];
            if (item.type == FieldType.text) {
              return BrnBaseTitle(
                title: item.label,
                themeData: theme,
                customActionWidget: SizedBox(
                  width: 200.w,
                  child: Text(
                    FuncUtils.isEmpty(item.extraText)
                        ? item.value
                        : '${item.value}(${item.extraText})',
                    style: TextStyles.f14,
                    textAlign: TextAlign.end,
                    maxLines: 2,
                  ),
                ),
              );
            } else if (item.type == FieldType.input) {
              return YtInput(
                height: 42,
                labelText: item.label,
                controller: TextEditingController(text: item.value),
                focusInputTextStyle: TextStyles.f14,
                border: InputBorder.none,
                labelTextStyle:
                    TextStyles.styleWith(14.sp, color: Colour.otherTextColor),
                inputTextStyle:
                    TextStyles.styleWith(14.sp, color: Colour.otherTextColor),
                // focusTextColor: Colour.otherTextColor,
                onChanged: (value) {
                  if (item.valueOnchange != null) {
                    item.valueOnchange!(value);
                  }
                },
              );
            } else {
              return BrnTextSelectFormItem(
                  title: item.label,
                  value: item.value,
                  onTap: item.onTap,
                  themeData: theme,
                  valueMaxLines: 2);
            }
          },
          separatorBuilder: (BuildContext context, int index) {
            return Gaps.line;
          },
        ),
      ),
      bottomNavigationBar: Container(
        color: Colour.white,
        padding: const EdgeInsets.only(bottom: 25),
        child: YtBrnBottomButtonPanel(
            enableMainButton: controller.isEnableSave,
            mainButtonName: ResKey.save.tr,
            mainButtonOnTap: () => controller.save(),
            secondaryButtonName: ResKey.cancel.tr,
            secondaryButtonBorder: Border.all(color: Colour.dividerColor),
            secondaryButtonOnTap: () => controller.cancelEdit()),
      ),
    );
  }
}
